package com.apply.service;

import com.apply.entity.SysAnnounce;
import com.apply.repository.AnnounceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author tianp
 */
@Service
public class AnnounceService {
    @Autowired
    private AnnounceRepository announceRepository;

    public SysAnnounce getAnnounce() {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        List<SysAnnounce> list = (List<SysAnnounce>) announceRepository.findAll(sort);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysAnnounce announce) {
        int flag = announceRepository.setAnnounceContent(announce.getId(), announce.getAnnounceInfo());
        if (flag == 1) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    public SysAnnounce save(SysAnnounce sysAnnounce) {
        return announceRepository.save(sysAnnounce);
    }
}
